package com.joinway.mobile.service

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service

import com.joinway.framework.data.jdbc.repository.TableRepository;
import com.joinway.framework.ecshop.bean.domain.User
import com.joinway.framework.ecshop.utils.EcShopUtils;

@Service
class EcShopService {

	@Autowired TableRepository repository
	
	User findUser(String userName, String password = null){
		String name = userName.toUpperCase()
		String sql = "select * from login_user where upper(user_name) = ?"
		List users = repository.find(User, sql, name)
		
		User user = users && users.size() > 0 ? users[0] : null
		
		if(user && password){
			String pwd = EcShopUtils.encrypt(password, user.ecSalt)
			user = user.password.equals(pwd) ? user : null
		}
		
		user
	}
	
}
